package bookmanagementsystem.gui;

import bookmanagementsystem.dao.UserDAO;
import bookmanagementsystem.model.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegistrationFrame extends JFrame {
    private JTextField userIDField;
    private JPasswordField passwordField;
    private JTextField genderField;
    private JTextField phoneNumberField;
    private JTextField emailField;
    private JButton registerButton;
    private JButton clearButton;

    public RegistrationFrame() {
        setTitle("图书管理系统 - 注册");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents();
        addComponentsToFrame();
        addActionListeners();
    }

    private void initComponents() {
        userIDField = new JTextField(20);
        passwordField = new JPasswordField(20);
        genderField = new JTextField(20);
        phoneNumberField = new JTextField(20);
        emailField = new JTextField(20);
        registerButton = new JButton("注册");
        clearButton = new JButton("清除");
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new GridLayout(6, 2, 10, 10));

        // 对标签设置水平对齐方式为右对齐
        JLabel userIDLabel = new JLabel("用户ID：");
        userIDLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(userIDLabel);
        panel.add(userIDField);

        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(passwordLabel);
        panel.add(passwordField);

        JLabel genderLabel = new JLabel("性别：");
        genderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(genderLabel);
        panel.add(genderField);

        JLabel phoneNumberLabel = new JLabel("手机号：");
        phoneNumberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(phoneNumberLabel);
        panel.add(phoneNumberField);

        JLabel emailLabel = new JLabel("邮箱：");
        emailLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(emailLabel);
        panel.add(emailField);

        panel.add(registerButton);
        panel.add(clearButton);

        getContentPane().add(panel);
    }

    private void addActionListeners() {
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userID = userIDField.getText();
                String password = new String(passwordField.getPassword());
                String gender = genderField.getText();
                String phoneNumber = phoneNumberField.getText();
                String email = emailField.getText();

                User user = new User(userID, password, gender, phoneNumber, email);
                UserDAO userDAO = new UserDAO();
                if (userDAO.registerUser(user)) {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "注册成功！");
                    // 注册成功后关闭注册窗口
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "注册失败，请检查输入信息是否正确。");
                }
            }
        });

        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userIDField.setText("");
                passwordField.setText("");
                genderField.setText("");
                phoneNumberField.setText("");
                emailField.setText("");
            }
        });
    }

    public static void main(String[] args) {
        RegistrationFrame registrationFrame = new RegistrationFrame();
    }
}